package com.example.test.dialogfactory;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.ejlchina.okhttps.HTTP;
import com.ejlchina.okhttps.Process;
import easier.app.x.CommonCode;
import easier.log.logger.Logger;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.io.File;

public class MainActivity70 extends AppCompatActivity {

    private TextView text;
    private TextView text2;
    private Button downLoad;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity70.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main70 );
        initView();
    }

    @Override
    protected void onResume() {

        super.onResume();
        String s = App.printActivity( MainActivity70.this );
        Logger.d( s );
    }

    private void initView() {

        text = (TextView) findViewById( R.id.text );
        text2 = (TextView) findViewById( R.id.text2 );

        text.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                MainActivity71.start( v.getContext() );
            }
        } );

        text2.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                MainActivity70.start( v.getContext() );
            }
        } );

        downLoad = (Button) findViewById( R.id.downLoad );
        downLoad.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                Observable.just(
                        "https://cos.pgyer.com/759d82c2bdf931aaae56c7f93ed0e866.apk?sign=071a0da9f0035f8ef77d05e2a6203930&t=1657075963&response-content-disposition=attachment%3Bfilename%3DTMS%E5%8F%B8%E6%9C%BA%E7%94%A8_1.1.1.debug47.apk"
                    )
                    .doOnNext( new Consumer< String >() {
                        @SuppressLint( "NewApi" )
                        @Override
                        public void accept( String s ) throws Throwable {

                            File file = CommonCode.createTempFile( v.getContext(), "app", ".apk" );

                            HTTP http = HTTP.builder().build();
                            http.sync( s )
                                .get()
                                .getBody()
                                .stepBytes( 1024 * 1024 )
                                .setOnProcess( new java.util.function.Consumer< Process >() {
                                    @Override
                                    public void accept( Process process ) {

                                        Logger.i( "%d/%d %f ", process.getDoneBytes(), process.getTotalBytes(),
                                            process.getRate() );
                                    }
                                } )
                                .toFile( file )
                                .setOnSuccess( new java.util.function.Consumer< File >() {
                                    @Override
                                    public void accept( File file ) {

                                        Logger.i( file.getAbsolutePath() );
                                    }
                                } ).start();
                        }
                    } )
                    .subscribeOn( Schedulers.io() )
                    .subscribe( new Observer< String >() {
                        @Override
                        public void onSubscribe( @NonNull Disposable d ) {

                        }

                        @Override
                        public void onNext( @NonNull String s ) {

                        }

                        @Override
                        public void onError( @NonNull Throwable e ) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    } );
            }
        } );
    }
}